package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SearchHistoryActivity extends AppCompatActivity {

    private static final String KEY_HISTORY = "history";

    private static final String PREFS_NAME = "SearchHistory";



    private SharedPreferences sharedPreferences;
    private RecyclerView searchHistoryRecyclerView;
    private SearchHistoryAdapter adapter;
    private List<String> searchHistory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_history);

        // 初始化 SharedPreferences
        sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

        // 初始化 RecyclerView
        searchHistoryRecyclerView = findViewById(R.id.searchHistoryRecyclerView);
        searchHistoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 加载搜索历史记录
        loadSearchHistory();

        // 设置 adapter
        adapter = new SearchHistoryAdapter(searchHistory, this::onSearchHistoryItemClicked);
        searchHistoryRecyclerView.setAdapter(adapter);

        // 设置清除搜索历史的点击事件
        FloatingActionButton clearHistoryButton = findViewById(R.id.clearHistoryButton);
        clearHistoryButton.setOnClickListener(v -> clearSearchHistory());
    }

    private void loadSearchHistory() {
        Set<String> historySet = sharedPreferences.getStringSet(KEY_HISTORY, new HashSet<>());
        searchHistory = new ArrayList<>(historySet);
    }

    private void clearSearchHistory() {
        sharedPreferences.edit()
                .remove(KEY_HISTORY)
                .apply();
        searchHistory.clear();
        adapter.notifyDataSetChanged();
    }

    private void onSearchHistoryItemClicked(String query) {
        // 执行搜索操作
        performSearch(query);
    }

    private void performSearch(String query) {
        // 执行搜索操作
        // ...
        saveSearchHistory(query);
    }

    private void saveSearchHistory(String query) {
        Set<String> historySet = sharedPreferences.getStringSet(KEY_HISTORY, new HashSet<>());
        List<String> historyList = new ArrayList<>(historySet);

        // 确保记录没有重复
        historyList.remove(query);
        historyList.add(query); // 添加到最后一个位置

        System.out.println("搜索历史：" + historyList);

        // 保存到 SharedPreferences
        sharedPreferences.edit()
                .putStringSet(KEY_HISTORY, new HashSet<>(historyList))
                .apply();
    }

    private static class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.ViewHolder> {

        private final List<String> searchHistory;
        private final OnSearchHistoryItemClickedListener listener;

        SearchHistoryAdapter(List<String> searchHistory, OnSearchHistoryItemClickedListener listener) {
            this.searchHistory = searchHistory;
            this.listener = listener;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_search_history, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            String query = searchHistory.get(position);
            holder.bind(query);
        }

        @Override
        public int getItemCount() {
            return searchHistory.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            private final TextView queryTextView;

            ViewHolder(@NonNull View itemView) {
                super(itemView);
                queryTextView = itemView.findViewById(R.id.queryTextView);
            }

            void bind(String query) {
                queryTextView.setText(query);
                itemView.setOnClickListener(v -> listener.onSearchHistoryItemClicked(query));
            }
        }

        interface OnSearchHistoryItemClickedListener {
            void onSearchHistoryItemClicked(String query);
        }
    }
}